package com.cmccgz.udacs_landlord_webapp.main.user.login;


import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.cmccgz.baselibrary.base.BaseFragment;
import com.cmccgz.baselibrary.utils.StringUtils;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.interfaces.OnGetSmsKeyListenter;
import com.cmccgz.udacs_landlord_webapp.observers.SmsObserver;
import com.cmccgz.udacs_landlord_webapp.utils.ViewHelper;

/**
 * @author INX
 * 忘记密码fragment
 */
public class ForgetPwdFragment extends BaseFragment implements LoginContract.ForgetView, View.OnClickListener, OnGetSmsKeyListenter {
    private LoginContract.ForgetPresenter presenter;
    private TextView tv_getCode;
    private EditText et_userName;
    private EditText et_new_pwd;
    private EditText et_new_pwd_re;
    private Button btn_ok;
    private EditText et_code;
    private CheckBox cb_show_hide;
    private SmsObserver smsObserver;

    public static  ForgetPwdFragment instance(){
        return new ForgetPwdFragment();
    }
    @Override
    protected int setLayoutId() {
        return R.layout.fragment_forget_pwd;
    }

    @Override
    protected void initView() {
        et_userName = ((EditText) findViewById(R.id.et_userName));
        et_new_pwd = ((EditText) findViewById(R.id.et_new_pwd));
        et_new_pwd_re = ((EditText) findViewById(R.id.et_new_pwd_re));
        et_code = ((EditText) findViewById(R.id.et_code));
        tv_getCode = ((TextView) findViewById(R.id.tv_getCode));
        tv_getCode.setOnClickListener(this);
        btn_ok = ((Button) findViewById(R.id.btn_ok));
        btn_ok.setOnClickListener(this);
        cb_show_hide = ((CheckBox) findViewById(R.id.cb_show_hide));
        cb_show_hide.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    et_new_pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    et_new_pwd_re.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else{
                    et_new_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    et_new_pwd_re.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }
        });
        ViewHelper viewHelper = ViewHelper.instance(this);
        if (viewHelper != null) {
            viewHelper.setTitle(R.string.title_pwd_forget);
            viewHelper.setBackTextVisable(true);
        }
        new ForgetPresenter(this);
        initDatas();
        smsObserver = new SmsObserver(new Handler(),mContext,this);
        mContext.getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, smsObserver);
    }

    /**
     * 初始化数据，获取手机号
     */
    private void initDatas() {
        Bundle arguments = getArguments();
        if (arguments!=null) {
            String userName = arguments.getString("userName");
            if (!StringUtils.isEmpty(userName)) {
                et_userName.setText(userName);
            }
        }
    }

    @Override
    public void onChange(String smsKey) {
        Log.e("INX", "onChange: smsKey="+smsKey );
    }

    /**
     * 修改密码成功
     */
    @Override
    public void forgetSuccess() {
        dismissLoadingDialog();
        showToast(R.string.tips_pwd_forget_success);
        mContext.onBackPressed();
    }

    /**
     * 修改失败
     * @param msg
     */
    @Override
    public void forgetError(String msg) {
        dismissLoadingDialog();
        showToast(msg);
    }

    /**
     * 等待验证码
     * @param second
     */
    @Override
    public void waitCode(int second) {
        tv_getCode.setEnabled(false);
        tv_getCode.setText(second+"秒");
        if (second==0) {
            resetCode();
        }
    }

    /**
     * 获取验证码失败
     * @param msg
     */
    @Override
    public void getCodeError(String msg) {
        resetCode();
        showToast(msg);
    }

    /**
     * 获取验证码成功
     */
    @Override
    public void getCodeSuccess() {

    }

    /**
     * 充值验证码状态
     */
    @Override
    public void resetCode() {
        tv_getCode.setEnabled(true);
        tv_getCode.setText(R.string.get_code);
    }

    @Override
    public void setPresenter(LoginContract.ForgetPresenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_back:
                mContext.onBackPressed();
                break;
            case R.id.tv_getCode:
                presenter.getCode(et_userName.getText().toString(),handler);
                break;
            case R.id.btn_ok:
                showLoadingDialog("正在重置密码...");
                presenter.forgetPwd(et_userName.getText().toString(),
                        et_new_pwd.getText().toString(),
                        et_new_pwd_re.getText().toString(),
                        et_code.getText().toString());
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (smsObserver!=null) {
            mContext.getContentResolver().unregisterContentObserver(smsObserver);
        }
    }

    @Override
    public void onDestroy() {
        presenter.onDestroy();
        super.onDestroy();
    }
}
